package com.ruoyi.web.controller.answerRecord;

import java.util.List;

import com.ruoyi.common.annotation.RateLimiter;
import com.ruoyi.common.enums.LimitType;
import com.ruoyi.common.utils.CodeGeneratorUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.answerRecord.domain.UserQuizAnswerRecord;
import com.ruoyi.answerRecord.service.IUserQuizAnswerRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

/**
 * 用户答题记录管理Controller
 * 
 * @author amy
 * @date 2025-06-12
 */
@RestController
@RequestMapping("/record/record")
@Tag(name = "【用户答题记录管理】管理")
public class UserQuizAnswerRecordController extends BaseController
{
    @Autowired
    private IUserQuizAnswerRecordService userQuizAnswerRecordService;

    /**
     * 查询全部用户答题记录
     */
    @Operation(summary = "查询全部用户答题记录")
    @PreAuthorize("@ss.hasPermi('record:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserQuizAnswerRecord userQuizAnswerRecord)
    {
        startPage();
        List<UserQuizAnswerRecord> list = userQuizAnswerRecordService.selectAllQuizAnswerRecordList(userQuizAnswerRecord);
        return getDataTable(list);
    }

    /**
     * 查询用户答题记录管理列表
     */
    @Operation(summary = "查询用户答题记录管理列表")
    @GetMapping("/userList")
    public TableDataInfo userList(UserQuizAnswerRecord userQuizAnswerRecord)
    {
        startPage();
        List<UserQuizAnswerRecord> list = userQuizAnswerRecordService.selectUserQuizAnswerRecordList(userQuizAnswerRecord);
        return getDataTable(list);
    }

    /**
     * 导出用户答题记录管理列表
     */
    @Operation(summary = "导出用户答题记录管理列表")
    @PreAuthorize("@ss.hasPermi('record:record:export')")
    @Log(title = "用户答题记录管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserQuizAnswerRecord userQuizAnswerRecord)
    {
        List<UserQuizAnswerRecord> list = userQuizAnswerRecordService.selectUserQuizAnswerRecordList(userQuizAnswerRecord);
        ExcelUtil<UserQuizAnswerRecord> util = new ExcelUtil<UserQuizAnswerRecord>(UserQuizAnswerRecord.class);
        util.exportExcel(response, list, "用户答题记录管理数据");
    }

    /**
     * 获取用户答题记录管理详细信息
     */
    @Operation(summary = "获取用户答题记录管理详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userQuizAnswerRecordService.selectUserQuizAnswerRecordById(id));
    }

    /**
     * 新增用户答题记录管理
     */
    @Operation(summary = "新增用户答题记录管理")
    @Log(title = "用户答题记录管理", businessType = BusinessType.INSERT)
    @RateLimiter(time = 10, count = 3, key = "quiz-answer", limitType = LimitType.IP)
    @PostMapping
    public AjaxResult add(@RequestBody UserQuizAnswerRecord userQuizAnswerRecord)
    {
        userQuizAnswerRecord.setRecordCode(CodeGeneratorUtil.generateCode());
        userQuizAnswerRecord.setCreatedBy(getUsername());
        return toAjax(userQuizAnswerRecordService.insertUserQuizAnswerRecord(userQuizAnswerRecord));
    }

    /**
     * 修改用户答题记录管理
     */
    @Operation(summary = "修改用户答题记录管理")
    @PreAuthorize("@ss.hasPermi('record:record:edit')")
    @Log(title = "用户答题记录管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserQuizAnswerRecord userQuizAnswerRecord)
    {
        userQuizAnswerRecord.setUpdatedBy(getUsername());
        return toAjax(userQuizAnswerRecordService.updateUserQuizAnswerRecord(userQuizAnswerRecord));
    }

    /**
     * 删除用户答题记录管理
     */
    @Operation(summary = "删除用户答题记录管理")
    @PreAuthorize("@ss.hasPermi('record:record:remove')")
    @Log(title = "用户答题记录管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable( name = "ids" ) Long[] ids) 
    {
        return toAjax(userQuizAnswerRecordService.deleteUserQuizAnswerRecordByIds(ids));
    }
}
